﻿using Flurl.Http;
using Pay.Core;
using Pay.DTO;
using Pay.DTO.cjpay;
using Pay.DTO.dto;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Pay.Service.Pay
{
    public class CjPaySign
    {
        private const string req_url = "http://yhkys.market.alicloudapi.com/communication/personal/1887";
        private const string app_code = "80cf9fbfc24c4038a452250f8351286a";
        public CjPaySign() { }

#if DEBUG
        public string api_url = "http://openpay.rtybesttop.net:8081";
        public const string key = "11fcb6533a4f3a01cd30ce048a2d9bbc";
        public const string partner = "1551874511007";
#else
        public string api_url = "http://openpay.rtybesttop.net:8081";
        public const string key = "11fcb6533a4f3a01cd30ce048a2d9bbc";
        public const string partner = "1551874511007";
#endif
        public async Task<T> PayGateway<T>(cj_req_base model, string url) where T : cj_res_base, new()
        {
            var log = IocHelper.Get<ILogHelper>("info");
            model.bind_no = partner;
            model.sign = SecureHelper.MD5(_sign(model));
            var json = model.ToJson();
            log.Info(json);

            try
            {
                var resp1 = await (api_url + url).PostJsonAsync(model).ReceiveString();
                log.Info(resp1);
                return resp1.ToObject<T>();
            }
            catch (FlurlHttpException ex)
            {
                log.Error(ex.Message);
                return null;
            }
        }

        public string _sign(cj_req_base model)
        {
            string json = model.ToJson();
            var collection = json.ToObject<SortedDictionary<string, string>>();
            collection.Remove("sign");
            string result = string.Empty;
            foreach (var kv in collection)
            {
                if (string.IsNullOrEmpty(kv.Value))
                    continue;
                result += kv.Value;
            }
            result = key + result;
            var log = IocHelper.Get<ILogHelper>("info");
            log.Info(result);
            return result;
        }

        public async Task<verify> CertifyVerify(certify_verify dto)
        {
            var log = IocHelper.Get<ILogHelper>("info");
            verify card = null;
            var auth = new auth_card_dto
            {
                acc_no = dto.account_no,
                idcard = dto.id_card,
                name = dto.account_name,
                mobile = dto.mobile
            };
            try
            {
                card = await req_url
                    .WithHeader("Accept-Charset", "UTF-8")
                    .WithHeader("Authorization", "APPCODE " + app_code)
                    .PostUrlEncodedAsync(auth).ReceiveJson<verify>();
                log.Info($"请求实名认证四要素，成功返回：{card.ToJson()}");
            }
            catch (Exception ex)
            {
                log.Info($"请求实名认证四要素异常：{ex.StackTrace}");
            }

            return card;
        }
    }
}
